﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Reflection;
using NBear.Mapping;

namespace Whir.BLL
{
    /// <summary>
    /// 业务逻辑类 应用于ezEIP新建模块
    /// </summary>
    public static class Common<T>
    {
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static bool Add(Object inputObj)
        {
            Type t = ((T)inputObj).GetType();

            string tableName = t.FullName.Split('.')[2].ToString();

            string strSQL = "INSERT INTO [{2}] ({0}) VALUES({1})";
            string strFields = "";
            string strValues = "";
            object value = "";

            foreach (PropertyInfo property in t.GetProperties())
            {
                if (property.Name.ToLower().IndexOf("_pid") < 0)//插入不需要自增ID
                {

                    Type type = property.PropertyType;

                    string originalType = type.FullName;                    

                    value = t.GetProperty(property.Name).GetValue((T)inputObj, null);

                    if (value != null)
                    {
                        strFields += property.Name + ",";

                        if (originalType == typeof(int).FullName)
                        {
                            strValues += value + ",";
                        }
                        else if (originalType == typeof(DateTime).FullName)
                        {
                            strValues += "'" + value + "',";
                        }
                        else if (originalType == typeof(String).FullName)
                        {
                            strValues += "'" + value + "',";
                        }
                        else if (originalType == typeof(Boolean).FullName)
                        {
                            value = (value == "False") ? "0" : "1";
                            strValues += value + ",";
                        }
                        else if (originalType == typeof(float).FullName)
                        {
                            strValues += "'" + value + "',";
                        }
                        else if (originalType == typeof(decimal).FullName)
                        {
                            strValues += value + ",";
                        }
                        else
                        {
                            strValues += "'" + value + "',";
                        }
                    }

                }
            }
            strFields = strFields.Substring(0, strFields.Length - 1);
            strValues = strValues.Substring(0, strValues.Length - 1);
            strSQL = string.Format(strSQL, strFields, strValues, tableName);

            int reslut = Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(strSQL);

            bool btn = (reslut == 1) ? true : false;

            return btn;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static bool Update(Object inputObj)
        {
            Type t = ((T)inputObj).GetType();

            string tableName = t.FullName.Split('.')[2].ToString();

            string strSQL = "UPDATE [{0}] SET {1} WHERE {0}_PID = {2}";
            string strValues = "";            
            object value = "";
            string pid = "";
            DateTime dt;

            foreach (PropertyInfo property in t.GetProperties())
            {
                if (property.Name.ToLower().IndexOf("_pid") > 0)
                {
                    pid = t.GetProperty(property.Name).GetValue((T)inputObj, null).ToString();
                }
                else
                {
                    value = t.GetProperty(property.Name).GetValue((T)inputObj, null);

                    if (value != null)//当有值时才更新
                    {
                        Type type = property.PropertyType;

                        string originalType = type.FullName;

                        if (originalType == typeof(int).FullName)
                        {
                            strValues += property.Name + "=" + value + ",";
                        }
                        else if (originalType == typeof(DateTime).FullName)
                        {
                            dt = (DateTime)value;
                            if (dt.Year > 1900)
                            {
                                strValues += property.Name + "='" + value + "',";
                            }
                        }
                        else if (originalType == typeof(String).FullName)
                        {
                            strValues += property.Name + "='" + value + "',";
                        }
                        else if (originalType == typeof(Boolean).FullName)
                        {
                            value = (value.ToString() == "False") ? "0" : "1";
                            strValues += property.Name + "=" + value + ",";
                        }
                        else if (originalType == typeof(float).FullName)
                        {
                            strValues += property.Name + "='" + value + "',";
                        }
                        else if (originalType == typeof(decimal).FullName)
                        {
                            strValues += property.Name + "=" + value + ",";
                        }
                        else
                        {
                            strValues += property.Name + "='" + value + "',";
                        }
                    }
                }
            }
            strValues = strValues.Substring(0, strValues.Length - 1);
            strSQL = string.Format(strSQL, tableName, strValues, pid);

            int reslut = Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(strSQL);

            bool btn = (reslut == 1) ? true : false;

            return btn;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static T GetModel(int pid)
        {
            T model = default(T);

            Type t = typeof(T);

            string tableName = t.FullName.Split('.')[2].ToString();

            string strSQL = "SELECT * FROM {0} WHERE {0}_PID = " + pid.ToString();

            strSQL = string.Format(strSQL, tableName);

            DataSet ds = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL);

            
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0].Rows[0];
                model = (T)ObjectConvertor.ToObject(typeof(DataRow), typeof(T), dr, model);
            }

            return model;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static bool Delete(int pid)
        {
            Type t = typeof(T);

            string tableName = t.FullName.Split('.')[2].ToString();

            string strSQL = "DELETE FROM {0} WHERE {0}_PID = " + pid.ToString();

            strSQL = string.Format(strSQL, tableName);

            int reslut = Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(strSQL);

            bool btn = (reslut == 1) ? true : false;

            return btn;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataTable GetList(string strWhere)
        {
            Type t = typeof(T);

            string tableName = t.FullName.Split('.')[2].ToString();

            string strSQL = "SELECT * FROM {0} WHERE 1=1 " + strWhere;

            strSQL = string.Format(strSQL, tableName);

            DataSet ds = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return (DataTable)null;
            }
        }


        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //    return dal.GetMaxId();
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(int TypeID)
        //{
        //    return dal.Exists(TypeID);
        //}

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        //{
        //    return dal.GetList(PageSize, PageIndex, strWhere);
        //}
        #endregion  成员方法
    }
}

